package com.cw.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cw.data.ConstantEnum;
import com.cw.data.Result;
import com.cw.pojo.TbUser;
import com.cw.service.UserService;
import com.cw.util.CookieUtils;
import com.cw.util.ShiroUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Controller
public class FrontEndController {

    @Reference
    private UserService userService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("/")
    public String defaultPage() {

        return "index";
    }

    @RequestMapping("toLogin")
    public String toLogin() {

        return "login";
    }

    @RequestMapping("toRegister")
    public String toRegister() {

        return "/register";
    }

    @RequestMapping("getMessageCode")
    @ResponseBody
    public Result getMessageCode(String phone) {

        try {

            rocketMQTemplate.convertAndSend("phoneCode", phone);

            return Result.result(ConstantEnum.SUCCESS).setData("信息发送成功!");
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR).setData("信息发送失败!");
        }
    }

    @RequestMapping("registerAccount")
    @ResponseBody
    public Result registerAccount(TbUser tbUser, String yzm) {

        try {

            String salt = ShiroUtils.generateSalt(6);

            tbUser.setSalt(salt);

            tbUser.setPassword(ShiroUtils.encryptPassword("MD5", tbUser.getPassword(), salt, 10));

            tbUser.setCreated(new Date());

            tbUser.setUpdated(new Date());

            tbUser.setSourceType("1");

            tbUser.setStatus("Y");

            String code = userService.registerAccount(tbUser, yzm);

            return Result.result(ConstantEnum.SUCCESS).setData(code);

        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR).setData("注册失败!");
        }
    }

    @RequestMapping("login")
    @ResponseBody
    public Result login(TbUser tbUser) {

        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(tbUser.getUsername(), tbUser.getPassword());

        try {

            subject.login(token);

            return Result.result(ConstantEnum.SUCCESS).setData("登录成功!");

        }catch (UnknownAccountException ue) {

            ue.printStackTrace();

            return Result.result(ConstantEnum.ERROR).setData("用户名或密码错误");
        }catch (IncorrectCredentialsException ie) {

            ie.printStackTrace();

            return Result.result(ConstantEnum.ERROR).setData("用户名或密码错误!");
        }
    }

    @RequestMapping("toIndexHome")
    public String toIndexHome() {

        return "home-index";
    }


    //添加cookie
    @RequestMapping("setCookie")
    @ResponseBody
    public String setCookie(HttpServletRequest request, HttpServletResponse response,String str){
        CookieUtils.setCookie(request,response,"myCookie",str);
        return "OK";
    }

    //查询cookie
    @RequestMapping("getCookie")
    @ResponseBody
    public String getCookie(HttpServletRequest request){
        String myCookie = CookieUtils.getCookieValue(request, "myCookie");
        return myCookie;
    }
    //删除cookie
    @RequestMapping("deleteCookie")
    @ResponseBody
    public String deleteCookie(HttpServletRequest request,HttpServletResponse response){
        CookieUtils.deleteCookie(request,response,"myCookie");
        return "OK";
    }
}
